<%@page import="java.sql.ResultSet"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import = "org.apache.commons.fileupload.FileUpload" %>
<%@ page import = "org.apache.commons.fileupload.DiskFileUpload" %>
<%@ page import = "org.apache.commons.fileupload.FileItem" %>
<%@ page import = "java.io.File" %>
<%@ page import = "java.util.UUID" %>
<%@ page import = "java.io.BufferedInputStream" %>
<%@ page import = "java.io.BufferedOutputStream" %>
<%@ page import = "java.io.FileOutputStream" %>
<%@ page import = "java.io.IOException" %>

<%@ page contentType="text/html; charset=UTF-8" import="com.mysql.jdbc.*"%>
<%@ page contentType="text/html; charset=UTF-8" import="java.sql.Connection"%>
<%@ page contentType="text/html; charset=UTF-8" import="java.sql.DriverManager"%>
<%@ page contentType="text/html; charset=UTF-8" import="java.sql.Statement"%>

<%!
	public int insertDB(String _strProjectId, String _strImage){
		Connection con;
	    Statement stmt;
	 	int ret = 0;
	    try
	    {
	    	Class.forName("com.mysql.jdbc.Driver").newInstance(); 
			Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mimo" ,"root" ,"d0o9o84");
	        
	        stmt = conn.createStatement();
	        
	        stmt.execute("insert into imagestore(projectId, imageId) values('" + _strProjectId + "','" + _strImage + "');");
	        stmt.close();
	        conn.close();
			
	        ret = 0;
	    }
	    catch( Exception ex )
	    {
	    	ex.printStackTrace();
	        
	    	ret = -1;
	    }
	
		return 0; 
	}
%>

<%

String strMsg = null;
boolean bFail = false;

session = request.getSession(true);
String strId = (String)session.getAttribute("id");
if(strId == null){
	strMsg = "로그인 세션이 끊어졌습니다. 로그인후 다시 시도해주십시오.";
	bFail = true;
}

String strProjectId = (String)session.getAttribute("projectId");
if(strProjectId == null){
	strMsg = "프로젝트가 선택되지 않았습니다. 프로젝트를 선택후 다시 시도해주심시오.";
	bFail = true;
}


if(bFail == false){
	if (FileUpload.isMultipartContent(request)) {
	    String temporaryDir = "e:/imageStore";
	    
	    DiskFileUpload fileUpload = new DiskFileUpload();
	    fileUpload.setRepositoryPath(temporaryDir);
	    
	    // 최대 1메가까지 업로드 가능
	    fileUpload.setSizeMax(1024 * 1024 * 10); 
	    // 한번에 100K 까지는 메모리에 저장.
	    fileUpload.setSizeThreshold(1024 * 1024 * 10); 
	    
	    /* FileItem의 리스트 */
	    java.util.List fileItemList = fileUpload.parseRequest(request);
	    for (int i = 0 ; i < fileItemList.size() ; i++) {
	        FileItem fileItem = (FileItem) fileItemList.get(i);
	        if (fileItem.isFormField() == false) {
	        	try {
	        		String strUUID = UUID.randomUUID().toString();
	                File uploadedFile = new File(temporaryDir, strUUID.toString());
	                fileItem.write(uploadedFile);
	                //이미지 db에 등록
	                insertDB(strProjectId, strUUID);
	            } catch(IOException ex) {
	                // 예외 처리
	            }
	        }
		}
	    
	    strMsg = "정상적으로 업로드 되었습니다.";
	}
}
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<div style='font-size:10pt;font-family:돋움;' align='center'>
<br/><br/><br/><br/>
<%=strMsg%><br/><br/><br/>
<input type='button' value="확인" onclick='window.close();' >
</div> 
</body>
</html>